package com.bocomm.tracevisualization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocomm.tracevisualization.api.entity.TraceTargetDataDetail;
import com.bocomm.tracevisualization.api.service.ITraceTargetDataDetailService;
import com.bocomm.tracevisualization.api.service.ITranslateManagementService;
import com.bocomm.tracevisualization.api.vo.resp.SequenceDiagram;
import com.bocomm.tracevisualization.mapper.TraceTargetDataDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.soap.Addressing;
import java.util.ArrayList;
import java.util.List;

import static com.bocomm.tracevisualization.core.CmdExecutor.fetchLocalHostIp;

/**
 * <p>
 * traceroute的每一行返回值 服务实现类
 * </p>
 *
 * @author cxp
 * @since 2024-03-20
 */
@Service
public class TraceTargetDataDetailServiceImpl extends ServiceImpl<TraceTargetDataDetailMapper, TraceTargetDataDetail> implements ITraceTargetDataDetailService {

    @Autowired
    ITranslateManagementService translateManagementService;
    public SequenceDiagram sequenceDiagram(String targetIp) {
        String sourceIP = fetchLocalHostIp();
        String jobId ="terminalTask."+sourceIP+"_"+targetIp;
        String targetHost = translateManagementService.aliasIp(targetIp);
        String sourceHost = translateManagementService.aliasIp(translateManagementService.aliasIp(sourceIP));
        List<TraceTargetDataDetail> traceTargetDataDetails = baseMapper.sequenceDiagram(jobId,fetchLocalHostIp());

        ArrayList<TraceTargetDataDetail> traceTarget = new ArrayList<>();
        traceTarget.add(TraceTargetDataDetail.defaultSourceData(sourceIP,sourceHost));
        traceTarget.addAll(traceTargetDataDetails);

        SequenceDiagram sequenceDiagram = new SequenceDiagram();
        sequenceDiagram.setDataDetails(traceTarget);
        sequenceDiagram.setTargetIp(targetIp);
        sequenceDiagram.setSourceIp(sourceIP);
        sequenceDiagram.setTargetHostName(targetHost);
        sequenceDiagram.setSourceHostName(sourceHost);
        return sequenceDiagram;
    }

}
